/*
 * $Id$
 */
package corejs.parser;

import static corejs.JsTokenType.*;
import corejs.Token;
import corejs.expr.Expression;
import corejs.expr.ObjectExpr;

import static corejs.parser.Precedence.SEQUENCE;

/**
 *
 * @author pengyu
 */
public class ObjectParser implements PrefixParser {

   @Override
   public Expression expr(final PrattParser parser, final Token tk) {
      final ObjectExpr expr = new ObjectExpr();
      while (!parser.match(RIGHT_BRACE)) {
         final Expression key = parser.expr();
         parser.nextToken(COLON);
         final Expression val = parser.expr(SEQUENCE); // to avoid COMMA parser
         expr.put(key.toString(), val);

         if (!parser.match(COMMA)) {
            parser.nextToken(RIGHT_BRACE);
            break;
         }
      }
      return expr;
   }
}
